//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <aura.h>
#include "private.h"
#include <pthread.h>
#include <stdio.h>

extern Aura_UInt32 g_ThreadDescrSlot;

AURA_API AuraTls_Allocate(
    /* [out] */ Aura_UInt32 * pIndex)
{
    pthread_key_t  key;
    int ret = 0;

    assert(NULL != pIndex);

    ret = pthread_key_create(&key, NULL);
    if (0 != ret) {
        return GetLastAuraECode();
    }

    *pIndex = (Aura_UInt32)(key);
    return AURA_NOERROR;
}

AURA_API AuraTls_Free(
    /* [in]  */ Aura_UInt32 uIndex)
{
    pthread_key_delete((pthread_key_t)(uIndex));
    return AURA_NOERROR;
}

AURA_API AuraTls_SetValue(
    /* [in]  */ Aura_UInt32 uIndex,
    /* [in]  */ Aura_PVoid pValue)
{
    Aura_Int32 ret = 0;
    Aura_ECode ec = AURA_NOERROR;

    ret = pthread_setspecific((pthread_key_t)uIndex, pValue);
    if (0 != ret) {
        ec = GetLastAuraECode();
    }

    return ec;
}

AURA_API AuraTls_GetValue(
    /* [in]  */ Aura_UInt32 uIndex,
    /* [out] */ Aura_PVoid * ppValue)
{
    Aura_ECode ec = AURA_NOERROR;
    Aura_PVoid pRet = NULL;

    assert(NULL != ppValue);

    pRet = pthread_getspecific((pthread_key_t)uIndex);
    if (NULL == pRet) {
        ec = GetLastAuraECode();
    }

    *ppValue = pRet;
    return ec;
}
